import QtQuick 1.0
import "../HttpAccess/"
import "js/TwitterAPI.js" as TwitterAPI

Item {

    //////////////////////////////////////////////////////////
    // properties

    // OAuth settings
    property string consumerSecret: ""
    property string consumerKey: ""

    // User Infomations
    property string oauthToken: ""
    property string oauthTokenSecret: ""
    property string screenName: ""
    property string userId: ""

    // Format
    // 0:xml, 1:json, 2:
    property int format: 0

    // Ready
//    property bool ready: true
    property alias ready: _httpAccess.ready


    //////////////////////////////////////////////////////////
    // methods

    function isNullOrEmpty(param){
        var ret = false;
        if(param === undefined){
            ret = true;
        }else if(param === null){
            ret = true;
        }else if(param.length === 0){
            ret = true;
        }else{
        }
        return ret;
    }

    //認証情報の状態を確認する
    function checkAuthInfo(){
        var ret = false;

        //check
        if((oauthToken.length === 0)
                || (oauthTokenSecret.length === 0)){
            //ng
        }else{
            //ok
            ret = true;
        }
        return ret;
    }

    //シグネチャをつけたURLを作成する
    function makeSignedURL(method, api, content) {

        var target = "";

        //check
        if(!checkAuthInfo()){
            // ng
            console.debug("nothing auth information.")
        }else{
            // ok
            var accessor = {
                consumerSecret: consumerSecret,
                tokenSecret: oauthTokenSecret
            };

            var message = {
              method: method,
              action: api,
              parameters: {
                oauth_signature_method: "HMAC-SHA1",
                oauth_consumer_key: consumerKey,
                oauth_token: oauthToken
              }
            };

            // add params
            for ( var key in content ) {
              message.parameters[key] = content[key];
            }
            // sign
            TwitterAPI.OAuth.setTimestampAndNonce(message);
            TwitterAPI.OAuth.SignatureMethod.sign(message, accessor);
            target = TwitterAPI.OAuth.addToURL(message.action, message.parameters);
        }

        return target;
    }

    // フォーマットを取得する
    function getFormat(){
        var ret = "";
        switch(format){
        case 0:
            ret = TwitterAPI.FORMAT_XML;
            break;
        case 1:
            ret = TwitterAPI.FORMAT_JSON;
            break;
        case 2:
            ret = TwitterAPI.FORMAT_ATOM;
            break;
        default:
            ret = TwitterAPI.FORMAT_XML;
            break;
        }
        return ret;
    }


    // APIのリクエスト
    function requestAPI(method, api_name, content, on_func, url_replace){
        if(false){//!ready){
            if(on_func === null || on_func === undefined){
            }else{
                on_func("");
            }
        }else{
            //ready = false;

            var url = getAPI(api_name);
            if(url_replace !== undefined){
                url = url.replace("%s", url_replace);
            }
            url = makeSignedURL(method, url, content, null);
            // TwitterAPI.requestHttpEx("", method, url, responseHttp, on_func);
            _httpAccess.requestHttp("", method, url, on_func);
//            console.debug(method + " url:" + url);
        }
    }

    // Http
    function requestHttp(data , method , url, on_func){
        // TwitterAPI.requestHttpEx("", method, url, responseHttp, on_func);
        _httpAccess.requestHttp("", method, url, on_func);
    }

    // APIのレスポンスの共通関数
    function responseHttp(object, on_func){
        //ready = true;

        var text = object.responseText;
        if(on_func === null || on_func === undefined){
        }else{
            on_func(text);
        }
    }


    // APIを取得する
    function getAPI(api_name){
        var api = TwitterAPI.URL_APIS[api_name];
        if(api.length > 0){
            api += getFormat();
        }
        return api;
    }

    //////////////////////////////////////////////////////////
    // elements

    HttpAccess{
        id: _httpAccess
    }

}
